package com.emenu.common;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.emenu.bean.PubTimer;

public class CM {
	/**
	 * 格式化数字
	 * @param d double
	 * @return String 返回具有2位小数的数字字符串
	 */
	public static String formatNumber(double d) {
		String retStr = "0.00";
		try {
			DecimalFormat dFormat = new DecimalFormat("#0.00");
			retStr = dFormat.format(d);
			return retStr;
		} catch (Exception e) {
			return retStr;
		}
	}
	
	/**
	 * 获取当前的格式化日期 	
	 * @return String 返回形式为"yyyy-MM-dd"的格式化日期
	 */
	public static String formatDate() {
		String retStr = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		retStr = sdf.format(new Date());
		return retStr;
	}

	/**
	 * 获取当前的格式化日期 	
	 * @param format String 指定的格式
	 * @return String 
	 */
	public static String formatDate(String format) {
		String retStr = "";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		retStr = sdf.format(new Date());
		return retStr;
	}
	
	
	public static String getTimeCode(List<PubTimer> list) {
		String time_code = "1";
		// 当前日期
		String date = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date dd = Calendar.getInstance().getTime();
		date = sdf.format(dd);		

		for (PubTimer pt : list) {

			if (isDateBefore((date + pt.getC2()))
					&& isDateAfter(date + pt.getC3())) {
				time_code = pt.getCode();

			}

		}

		return time_code;

	}

	// 固定午市晚市，不推荐使用
	public static int getTimeCode() {
		int time_code = 1;
		// 时间比较，显示当前市别
		String date = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date dd = Calendar.getInstance().getTime();
		date = sdf.format(dd);

		boolean flag = isDateBefore(date + " 15:01:00");
		System.out.println(flag);
		if (flag) {
			time_code = 1;

		} else {
			time_code = 2;

		}
		return time_code;
	}

	public static boolean isDateBefore(String date2) {
		try {
			Date date1 = new Date();
			DateFormat df = DateFormat.getDateTimeInstance();
			System.out.println(date1.before(df.parse(date2)));
			return date1.before(df.parse(date2));
		} catch (ParseException e) {
			System.out.print(e.getMessage());
			return false;
		}

	}

	public static boolean isDateAfter(String date2) {
		try {
			Date date1 = new Date();
			DateFormat df = DateFormat.getDateTimeInstance();
			System.out.println(date1.after(df.parse(date2)));
			return date1.after(df.parse(date2));
		} catch (ParseException e) {
			System.out.print(e.getMessage());
			return false;
		}
	}
	

}
